<template>
  <div>
    <el-cascader
      v-model="pid"
      :options="list"
      :props="props"
      clearable
      expand-trigger="hover"
    />

  </div>
</template>
<script>
import {
  findAllPermissions as findAllPermissionsApi
} from '@/api/auth/source'
// Load the full build.
var _ = require('lodash') // 要求熟练使用
export default {
  name: 'Source',
  mixins: [],
  data() {
    return {
      pid: '',
      meunList: [],
      props: {
        label: 'title',
        value: 'id',
        checkStrictly: true,
        emitPath: false
      }
    }
  },
  computed: {
    list() {
      const cloneDeepMeunList = _.cloneDeep(this.meunList)
      this.buildList(cloneDeepMeunList)
      return cloneDeepMeunList
    }
  },
  watch: {
  },
  created() {
    this.getPermission()
  },
  mounted() {
  },
  methods: {
    async getPermission() {
      const res = await findAllPermissionsApi()
      const { success, data, message } = res
      if (success) {
        this.meunList = data.menus
      } else {
        this.$message.error(message)
      }
    },
    // 递归
    buildList(arr) {
      for (let i = 0; i < arr.length; i++) {
        const item = arr[i]
        if (item.type === 2) {
          item.disabled = true
        }
        if (item.children && item.children.length > 0) {
          this.buildList(item.children)
        } else {
          delete item.children
        }
      }
    }
  }

}
</script>
<style lang="scss" scoped>
</style>
